package com.my.study.filter;

import org.apache.commons.text.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/4/19 7:31
 **/
public class TestXss {

    private static final Whitelist JSOUP_WHITE_LIST = Whitelist.relaxed();
    private static final Document.OutputSettings JSOUP_OUTPUT_SETTINGS = new Document.OutputSettings();

    static {
        /**
         * addTags() 设置白名单标签
         * addAttributes()  设置标签需要保留的属性 ,[:all]表示所有
         * preserveRelativeLinks()  是否保留元素的URL属性中的相对链接，或将它们转换为绝对链接,默认为false. 为false时将会把baseUri和元素的URL属性拼接起来
         */
        JSOUP_WHITE_LIST.preserveRelativeLinks(true);
    }


    public static void main(String[] args) {
        String html = "<a href='http://www.baidu.com'>《百度链接》&^><</a>";
        String l = "<p><a href=\"www.test.xhtml\">test</a><a href=\"/abc.com\">ttt</a><a href=\"http://www.test.xhtml\">ggg</a>";
        String h1 =  "<p是事实上事实上事实 <span>span标签</span><h1>一级标题</h1>";
        String result = Jsoup.clean(html, "", JSOUP_WHITE_LIST, JSOUP_OUTPUT_SETTINGS);
        String s = StringEscapeUtils.unescapeHtml4(result);
        System.out.println(result);
        System.out.println(s);
    }

}
